﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Bean;
using Shpm.App.Input.Dao;
using Shpm.App.Master.Bean;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Commons.Utils;
using System.Reflection;
using Shpm.System.Web.Control;
using log4net;
using Shpm.System.Validator;

public partial class App_Input_PersonLeave : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Context.Items["LEAVE_ID"] != null)
            {
                ViewState["LEAVE_ID"] = Context.Items["LEAVE_ID"];

            }
            else
            {
                ViewState["LEAVE_ID"] = null;
            }
            //ViewState["LEAVE_ID"] = 1;

            initDic();
            if (Context.Items.Contains("CMB_GROUP_ID"))
            {
                this.ViewState["GROUP_ID"] = Convert.ToString(Context.Items["CMB_GROUP_ID"]);
            }
            if (Context.Items.Contains("CMB_JOB_ID"))
            {
                this.ViewState["JOB_ID"] = Convert.ToString(Context.Items["CMB_JOB_ID"]);
            }
            if (Context.Items.Contains("CMB_EMPLOYEE_ID"))
            {
                this.ViewState["EMPLOYEE_ID"] = Convert.ToString(Context.Items["CMB_EMPLOYEE_ID"]);
            }
            if (Context.Items.Contains("CMB_EMPLOYEE_NAME"))
            {
                this.ViewState["EMPLOYEE_NAME"] = Convert.ToString(Context.Items["CMB_EMPLOYEE_NAME"]);
            }
            if (Context.Items.Contains("CMB_BEGIN_DATE"))
            {
                this.ViewState["BEGIN_DATE"] = Convert.ToString(Context.Items["CMB_BEGIN_DATE"]);
            }
            if (Context.Items.Contains("CMB_END_DATE"))
            {
                this.ViewState["END_DATE"] = Convert.ToString(Context.Items["CMB_END_DATE"]);
            }
            
                setApprove();
            
            
        }
        if (!string.IsNullOrEmpty(Convert.ToString(ViewState["LEAVE_ID"])))
        {
            PersonLeaveBean plBean = new PersonLeaveBean();
            plBean.LeaveId = Convert.ToInt32(ViewState["LEAVE_ID"]);
            PersonLeaveDao plDao = new PersonLeaveDao();
            plBean = plDao.Find(plBean);
            SysUserManager manager = new SysUserManager(Context);
            string userId = manager.getSysUserBean().getMstSysUserBean().SysUserId;
            if (plBean.AuditingId == userId && plBean.AuditingFlg == false && plBean.CommitFlg == true)
            {
                Button3.Enabled = true;
            }
            else
            {
                Button3.Enabled = false;
            }
        }
    }

    /*数据字典数据初始化*/
    private void initDic()
    {

        CommonDictionaryBean paramBean = new CommonDictionaryBean();
        CommonDictionaryDao dao = new CommonDictionaryDao();
        /*申请假别*/
        paramBean.Catalog = "T_LEAVE.LEAVE_TYPE";
        initAll(dao, paramBean, this.LeaveType);
    }

    private void initAll(CommonDictionaryDao dao, CommonDictionaryBean paramBean, ListControl obj)
    {
        IList list = null;
        list = dao.FindByCatalog(paramBean);

        obj.DataSource = list;
        obj.DataBind();
    }

    private void setApprove()
    {
        //审核人列表
        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        MstSysUserBean mstSysUserBean =
            sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
        MstEmployeeBean mstEmpBean =
            sysUserBean == null ? null : sysUserBean.getEmployeeBean();
        string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
        string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;

        //检索部门编号
        PersonEmployeeBean employbean = new PersonEmployeeBean();
        ApproveDao employApplyDao = new ApproveDao();
        if (string.IsNullOrEmpty(Convert.ToString(ViewState["LEAVE_ID"])))
        {
            employbean.EmployeeId = emplyId;
        }
        else
        {
            PersonLeaveBean plBean = new PersonLeaveBean();
            plBean.LeaveId = Convert.ToInt32(ViewState["LEAVE_ID"]);
            PersonLeaveDao plDao = new PersonLeaveDao();
            plBean = plDao.Find(plBean);
            employbean.EmployeeId = plBean.EmployeeId;
        }
        employbean = employApplyDao.CheckAuthority(employbean);

        EmployeeBean employeeBean = new EmployeeBean();

        employeeBean.DepartId = employbean.DepartId;
        employeeBean.JobLimit = employbean.JobLimit;

        IList list = new ArrayList();

        list = employApplyDao.FindCheckEmp(employeeBean);

        DropDownList1.DataSource = list;
        DropDownList1.DataBind();
    }
    private void setApprove1()
    {

    }
    private void SaveBase()
    {
        SysUserManager manager = new SysUserManager(Context);
        string userId = manager.getSysUserBean().getMstSysUserBean().SysUserId;
        string emId = manager.getSysUserBean().getMstSysUserBean().EmployeeId;

        PersonLeaveBean dataBean = new PersonLeaveBean();
        PersonLeaveDao Dao = new PersonLeaveDao();
        dataBean.EmployeeId = hfEmployeeId.Value;
        dataBean.RequestId = hfEmployeeId.Value;
        dataBean.LeaveType = LeaveType.SelectedValue;
        dataBean.LeaveMemo = LeaveMemo.Text;
        dataBean.BeginDate = DateUtils.convertToDate(this.BeginDate.Text);
        dataBean.EndDate = DateUtils.convertToDate(this.EndDate.Text);
        dataBean.BeginTime = BeginTime.Text;
        dataBean.EndTime = EndTime.Text;
        dataBean.LeaveDay = LeaveDay.Text;
        dataBean.LeaveHour=LeaveHour.Text;
        dataBean.UserId = userId;
        dataBean.AuditingId = DropDownList1.SelectedValue;
        dataBean.AuditingFlg = false;
        EmployeeBean eBean = new EmployeeBean();
        EmployeeDao eDao = new EmployeeDao();
        eBean.EmployeeId = hfEmployeeId.Value;
        eBean=eDao.GetEmployee(eBean);
        string employeeName = eBean.EmployeeName; 
        if (ViewState["LEAVE_ID"] != null)
        {
            if ( Convert.ToString(ViewState["STATUS"]) == "Upd")
            {
                dataBean.CommitFlg = true;
                dataBean.LeaveId = Convert.ToInt32(ViewState["LEAVE_ID"]);
                Dao.Update(dataBean);
                SendMessage(employeeName + "的请假申请已经确认,烦请领导审批！", this.DropDownList1.SelectedValue);
            }
            else if (Convert.ToString(ViewState["STATUS"]) == "Audit")
            {
                dataBean.AuditingFlg = true;
                dataBean.LeaveId = Convert.ToInt32(ViewState["LEAVE_ID"]);
                //dataBean.AuditingId = emId;
                Dao.UpdateAuditing(dataBean);
                SendMessage(employeeName + "的请假申请已经审批完毕!", hfEmployeeId.Value);
            }
            else if (Convert.ToString(ViewState["STATUS"]) == "Add")
            {
                dataBean.CommitFlg = false;
                dataBean.LeaveId = Convert.ToInt32(ViewState["LEAVE_ID"]);
                Dao.Update(dataBean);
            }
        }
        else
        {
            ViewState["LEAVE_ID"]=(int)Dao.Insert(dataBean);
            
        }
        ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();
    }

    void Page_PreRender(object sender, EventArgs e)
    {
        Button1.Enabled = false;
        Button2.Enabled = false;
        Button3.Enabled = false;
        SysUserManager manager = new SysUserManager(Context);
        string userId = manager.getSysUserBean().getMstSysUserBean().SysUserId;
        string emId = manager.getSysUserBean().getMstSysUserBean().EmployeeId;
        PersonEmployeeBean logonBean = new PersonEmployeeBean();
        PersonEmployeeDao logonDao = new PersonEmployeeDao();
        logonBean.EmployeeId = emId;
        logonBean = logonDao.Find(logonBean);
        hfEmployeeId.Value = emId;
        if (ViewState["LEAVE_ID"] != null)
        {
            PersonLeaveBean plBean = new PersonLeaveBean();
            plBean.LeaveId = Convert.ToInt32(ViewState["LEAVE_ID"]);
            PersonLeaveDao plDao = new PersonLeaveDao();
            plBean = plDao.Find(plBean);
            if (plBean != null)
            {
                LeaveType.SelectedValue = plBean.LeaveType;
                LeaveMemo.Text = plBean.LeaveMemo;
                BeginDate.Text = plBean.BeginDate == null ? "" : DateUtils.convertToString((DateTime)plBean.BeginDate);
                EndDate.Text = plBean.EndDate == null ? "" : DateUtils.convertToString((DateTime)plBean.EndDate);
                BeginTime.Text = plBean.BeginTime;
                EndTime.Text = plBean.EndTime;
                LeaveDay.Text = plBean.LeaveDay;
                LeaveHour.Text = plBean.LeaveHour;
                hfEmployeeId.Value = plBean.EmployeeId;
                PersonEmployeeBean dataBean = new PersonEmployeeBean();
                dataBean.EmployeeId = plBean.EmployeeId;
                dataBean = logonDao.Find(dataBean);
                this.EmployeeId.Text = dataBean.EmployeeName;
                this.group.Text = dataBean.GroupName;
                this.job.Text = dataBean.JobName;
                DropDownList1.SelectedValue = plBean.AuditingId;
                if (plBean.AuditingFlg)
                {
                    Button1.Enabled = false;
                    Button2.Enabled = false;
                    Button3.Enabled = false;
                    status.Text = "已审批";
                }
                //else if (plBean.CommitFlg)
                //{
                //    if(plBean.EmployeeId == emId)
                //    {
                //        this.Button1.Enabled = true;
                //        this.Button2.Enabled = true;
                //    }
                //    status.Text = "已保存";
                //}
                else if (!plBean.CommitFlg)
                {
                    if (plBean.EmployeeId == emId)
                    {
                        this.Button1.Enabled = true;
                        this.Button2.Enabled = true;
                    }
                    status.Text = "未提交";
                }
                else
                {
                    Button1.Enabled = false;
                    Button2.Enabled = false;
                    if (logonBean.GroupId == "2")
                    {
                        this.Button3.Enabled = true;
                        status.Text = "已提交";
                    }
                    else if (Convert.ToInt32(logonBean.JobLimit) > Convert.ToInt32(dataBean.JobLimit) && logonBean.GroupId == dataBean.GroupId)
                    {
                        this.Button3.Enabled = true;
                        status.Text = "已提交";
                    }
                    else
                    {
                        Button1.Enabled = false;
                        Button2.Enabled = false;
                        Button3.Enabled = false;
                        status.Text = "已提交";
                    }
                }
            }
        }
        else
        {
            if (logonBean != null)
            {

                this.EmployeeId.Text = logonBean.EmployeeName;
                this.group.Text = logonBean.GroupName;
                this.job.Text = logonBean.JobName;
                Button1.Enabled = true;
                status.Text = "未保存";
            }
        }
        if (!string.IsNullOrEmpty(Convert.ToString(ViewState["LEAVE_ID"])))
        {

            PersonLeaveBean pelBean = new PersonLeaveBean();
            pelBean.LeaveId = Convert.ToInt32(ViewState["LEAVE_ID"]);
            PersonLeaveDao pelDao = new PersonLeaveDao();
            pelBean = pelDao.Find(pelBean);
            //SysUserManager manager = new SysUserManager(Context);
            //string userId = manager.getSysUserBean().getMstSysUserBean().SysUserId;
            if (pelBean.AuditingId == userId && pelBean.AuditingFlg == false && pelBean.CommitFlg == true)
            {
                Button3.Enabled = true;
            }
            else
            {
                Button3.Enabled = false;
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (ComValidator1.DoValidator())
        {
            ComValidateChecker vd = new ComValidateChecker();
            if (!string.IsNullOrEmpty(BeginTime.Text))
            { vd.Add(new CheckMatches(BeginTime.Text, Constants.CHECK_REGEX_TIME_HOURMINUTE, new ComMessage("E0034", "开始时间"))); }

            if (!string.IsNullOrEmpty(EndTime.Text))
            { vd.Add(new CheckMatches(EndTime.Text, Constants.CHECK_REGEX_TIME_HOURMINUTE, new ComMessage("E0034", "结束时间"))); }
            if (!vd.DoValidator())
            {
                ((Design_MasterPage)this.Master).InfoMessage = vd.ErrorMessage;
                return;
            }
            if (this.BeginDate.Text.Equals(this.EndDate.Text))
            {
                if (this.BeginTime.Text.CompareTo(this.EndTime.Text) > 0)
                {
                    string ErrMessage = "结束时间不能小于开始时间";
                    ((Design_MasterPage)this.Master).InfoMessage = ErrMessage;
                    return;
                }

            }
            ViewState["STATUS"] = "Upd";
            SaveBase();
            

        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (ComValidator1.DoValidator())
        {
            ComValidateChecker vd = new ComValidateChecker();
            if (!string.IsNullOrEmpty(BeginTime.Text))
            { vd.Add(new CheckMatches(BeginTime.Text, Constants.CHECK_REGEX_TIME_HOURMINUTE, new ComMessage("E0034", "开始时间"))); }

            if (!string.IsNullOrEmpty(EndTime.Text))
            { vd.Add(new CheckMatches(EndTime.Text, Constants.CHECK_REGEX_TIME_HOURMINUTE, new ComMessage("E0034", "结束时间"))); }
            if (!vd.DoValidator())
            {
                ((Design_MasterPage)this.Master).InfoMessage = vd.ErrorMessage;
                return;
            }
            if (this.BeginDate.Text.Equals(this.EndDate.Text))
            {
                if (this.BeginTime.Text.CompareTo(this.EndTime.Text) > 0)
                {
                    string ErrMessage = "结束时间不能小于开始时间";
                    ((Design_MasterPage)this.Master).InfoMessage = ErrMessage;
                    return;
                }

            }
            ViewState["STATUS"] = "Audit";
            SaveBase();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (ComValidator1.DoValidator())
        {
            ComValidateChecker vd = new ComValidateChecker();
            if (!string.IsNullOrEmpty(BeginTime.Text))
            { vd.Add(new CheckMatches(BeginTime.Text, Constants.CHECK_REGEX_TIME_HOURMINUTE, new ComMessage("E0034", "开始时间"))); }

            if (!string.IsNullOrEmpty(EndTime.Text))
            { vd.Add(new CheckMatches(EndTime.Text, Constants.CHECK_REGEX_TIME_HOURMINUTE, new ComMessage("E0034", "结束时间"))); }
            if (!vd.DoValidator())
            {
                ((Design_MasterPage)this.Master).InfoMessage = vd.ErrorMessage;
                return;
            }
            if (this.BeginDate.Text.Equals(this.EndDate.Text))
            {
                if (this.BeginTime.Text.CompareTo(this.EndTime.Text) > 0)
                {
                    string ErrMessage = "结束时间不能小于开始时间";
                    ((Design_MasterPage)this.Master).InfoMessage = ErrMessage;
                    return;
                }

            }
            ViewState["STATUS"] = "Add";
            SaveBase();
        }
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        string directUrl = "~/App/Input/PersonLeaveSrhForm.aspx";
        Context.Items["ParamPageIndex"] = this.ViewState["ParamPageIndex"];
        Context.Items["CMB_JOB_ID"] = Convert.ToString(this.ViewState["JOB_ID"]);
        Context.Items["CMB_GROUP_ID"] = Convert.ToString(this.ViewState["GROUP_ID"]);
        Context.Items["CMB_EMPLOYEE_ID"] = Convert.ToString(this.ViewState["EMPLOYEE_ID"]);
        Context.Items["CMB_EMPLOYEE_NAME"] = Convert.ToString(this.ViewState["EMPLOYEE_NAME"]);
        Context.Items["CMB_BEGIN_DATE"] = Convert.ToString(this.ViewState["BEGIN_DATE"]);
        Context.Items["CMB_END_DATE"] = Convert.ToString(this.ViewState["END_DATE"]);
        ControllerUtils.Transfer(this.Context, directUrl, false);
    }

    /*名称： SendMessage
     *功能： 发送短信
     *参数： msg 短信内容, employeeId 短信内容地址
     *返回： 无
     *说明： 无
     */
    private void SendMessage(string msg, string employeeId)
    {
        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        MstSysUserBean mstSysUserBean =
            sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
        MstEmployeeBean mstEmpBean =
            sysUserBean == null ? null : sysUserBean.getEmployeeBean();
        string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
        string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;

        MessageBean beanMsg = new MessageBean();
        beanMsg.Subject = "请假单申请确认" + DateUtils.convertToString(DateTime.Now);
        beanMsg.Message = DateUtils.convertToString(DateTime.Now) + msg;
        beanMsg.MessageType = 3;
        beanMsg.SendDate = DateTime.Now;
        beanMsg.ReplyFlag = false;
        beanMsg.ReaderHistoryFlag = false;
        beanMsg.ImportantFlag = false;
        beanMsg.EmployeeId = emplyId;
        beanMsg.UserId = loginId;
        CommonSendMessageUtils sendMsg = new CommonSendMessageUtils(beanMsg, employeeId);
        sendMsg.SendMessage();
    }
}
